package com.flashphoner.click2call
{
	import com.flashphoner.Logger;
	import com.flashphoner.api.Flash_API;
	import com.flashphoner.click2call.views.ConnectingView;
	
	import flash.display.DisplayObject;
	import flash.utils.setTimeout;
	
	import mx.core.Application;
	import mx.managers.PopUpManager;
		
	public class ViewControllerClick2Call
	{
		private var connectingView:ConnectingView;
					
		public function ViewControllerClick2Call()
		{
		}

		public function failRegisterView():void{
			if (connectingView!=null){
				Logger.info("PhoneView.failRegisterView()");
				connectingView.text="Register fail";
				setTimeout(disconnect,3000);
			}	
		}
		
		public function sipPortsBusyView():void{
			showConnectingView();
			if (connectingView!=null){
				Logger.info("PhoneView.sipPortsBusyView()");
				connectingView.text="All sip ports is busy";
				connectingView.beClosed = true;
				setTimeout(hideConnectingView,3000);		
			}
		}
		
		public function wrongSipProviderAddressView():void{
			showConnectingView();
			if (connectingView!=null){
				Logger.info("PhoneView.wrongSipProviderAddressView()");
				connectingView.text="Wrong sip provider address";
				connectingView.beClosed = true;
				setTimeout(hideConnectingView,3000);		
			}
		}		
		
		public function waitingForRegisteredConnectionView():void{
			if (connectingView!=null){
				Logger.info("PhoneView.waitingForRegisteredConnectionView()");
				connectingView.text="Waiting for registered event...";
			}	
		}
		
		public function hideConnectingView():void{
			if (connectingView!=null){
				PopUpManager.removePopUp(connectingView);
				connectingView=null;
				
			}
		}
		
		public function showConnectingView():void{
			if (connectingView==null){
				connectingView = new ConnectingView();
				PopUpManager.addPopUp(connectingView,DisplayObject(Application.application),true);
				PopUpManager.centerPopUp(connectingView);
			}
				
		}
		
		public function closedConnectingView():void{
			if (connectingView!=null){
				connectingView.text="Connection closed success";
				setTimeout(hideConnectingView,3000);		
			}				
		}
		
		public function calleeNotFoundView():void{
			showConnectingView();
			if (connectingView!=null){
				connectingView.text="Callee not found!";
				setTimeout(hideConnectingView,3000);		
			}	
		}
		
		public function mediaPortsBusyView():void{
			showConnectingView();
			if (connectingView!=null){
				Logger.info("PhoneView.mediaPortsBusyView()");
				connectingView.text="All media ports is busy";
				setTimeout(hideConnectingView,3000);		
			}
		}
		
		
		public function failConnectionView():void{
			if (connectingView!=null){
				Logger.info("PhoneView.failConnectionView()");
				connectingView.text="Connection fail";
				setTimeout(hideConnectingView,3000);
			}	
		}
		
		public function registerAttemptExpire():void{
			if (connectingView!=null){
				Logger.info("PhoneView.registerAttemptExpire()");
				connectingView.text="Register fail, check SIP account settings";
				setTimeout(disconnect,3000);
			}			
		}
		
		private function disconnect():void{
			Flash_API.getInstance().logoff();
		}
		
	}
}